package com.example.konwRice.index.Dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


import com.example.konwRice.index.Bean.NewsBean;
import com.example.konwRice.index.Util.DBHelper;

import java.util.ArrayList;


public class HistoryDao {
    private DBHelper dbHelper;

    public HistoryDao(Context context) {
        dbHelper=new DBHelper(context,DBHelper.DATABASE_NAME,null,DBHelper.DATABASE_VERSION);
    }
    public void insertHistory(NewsBean news){
        SQLiteDatabase db=dbHelper.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put("title",news.getTitle());
        values.put("time",news.getTime());
        values.put("url",news.getUrl());
        values.put("imageUrl",news.getImageUrl());
        values.put("flag",news.getFlag());
        db.insert("history",null,values);
    }

    public ArrayList<NewsBean> queryHistory(){
        ArrayList<NewsBean> newsBeans=new ArrayList<NewsBean>();
        SQLiteDatabase db=dbHelper.getReadableDatabase();
        Cursor cursor=db.query("history",null,null,null,null,null,null,null);
        if(cursor.moveToFirst()){
            do{
                NewsBean newsBean=new NewsBean();
                newsBean.setTitle(cursor.getString(cursor.getColumnIndex("title")));
                newsBean.setType(cursor.getString(cursor.getColumnIndex("type")));
                newsBean.setTime(cursor.getString(cursor.getColumnIndex("time"))); ;
                newsBean.setUrl(cursor.getString(cursor.getColumnIndex("url")));
                newsBean.setImageUrl(cursor.getString(cursor.getColumnIndex("imageUrl")));
                newsBean.setFlag(cursor.getInt(cursor.getColumnIndex("flag")));
                newsBeans.add(newsBean);
            } while (cursor.moveToNext());
        }
        return newsBeans;
    }
}

